package com.lgh.springframework.beans.factory.support;

import com.lgh.springframework.beans.BeansException;
import com.lgh.springframework.beans.factory.config.BeanDefinition;
import com.lgh.springframework.beans.factory.config.SingletonBeanRegistry;

import java.util.HashMap;
import java.util.Map;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname DefaultListableBeanFactory
 * @description beanDefinition的注册和获取
 * @date 2023/02/21 20:56
 * <p>
 * ******************************************************
 */
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry {

    private final String NO_BEAN_NAMED = "No bean named '";
    private final String IS_DEFINED = "' is defined";

    private Map<String, BeanDefinition> beanDefinitionMap = new HashMap<>();

    @Override
    protected BeanDefinition getBeanDefinition(String beanName) throws BeansException {
        BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
        if (beanDefinition == null) {
            throw new BeansException(NO_BEAN_NAMED + beanName + IS_DEFINED);
        }
        return beanDefinition;
    }

    @Override
    public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
        beanDefinitionMap.put(beanName, beanDefinition);
    }
}
